package com.vq3.digitalizationsoftwareintegrates.Servlet.User;


import com.vq3.digitalizationsoftwareintegrates.Entity.Info.ResultInfo;
import com.vq3.digitalizationsoftwareintegrates.Service.User.UserLoginService;
import com.vq3.digitalizationsoftwareintegrates.Utils.PackagedDataUtil.Impl.PackagedDataImpl;
import com.vq3.digitalizationsoftwareintegrates.Utils.PackagedDataUtil.PackagedData;
import com.vq3.digitalizationsoftwareintegrates.Utils.PermissionControl.RegularValidation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@RestController
@RequestMapping("register")
public class RegisterServlet {
    @Resource
    UserLoginService userLoginService;


    @RequestMapping("checkUserID")
    public void checkUserID(@RequestParam String userId, HttpServletResponse httpServletResponse, HttpServletRequest httpServletRequest) throws IOException {
        boolean b = userLoginService.checkUserId(userId);
        PackagedData packagedData = new PackagedDataImpl();
        ResultInfo resultInfo = new ResultInfo();
        resultInfo.setFlag(b);
        packagedData.writeValue(resultInfo,httpServletResponse);
    }

    @RequestMapping("saveUser")
    public void saveUser(@RequestParam String userId,@RequestParam String pw,@RequestParam String rePw,HttpServletResponse httpServletResponse,HttpServletRequest httpServletRequest) throws IOException {
        PackagedData packagedData = new PackagedDataImpl();
        ResultInfo resultInfo = new ResultInfo();
        if (RegularValidation.registerVerify(userId) && RegularValidation.registerVerify(pw) && RegularValidation.registerVerify(rePw) && (pw.equalsIgnoreCase(rePw))) {
            boolean b = userLoginService.saveUser(userId, pw);
        }else {
            resultInfo.setFlag(false);
            resultInfo.setErrorMsg("注册失败！");
            packagedData.writeValue(resultInfo,httpServletResponse);
        }
    }
}
